package search

import (
    "strconv"
    "container/vector"
    "strings"
    "fmt"
    "io/ioutil"
)
type Bcv struct {
    Id string
    Book string
    Chapter int 
    Verse int 
    Text string
}


func (bcv* Bcv) GetMap() (map[string]string){
    return map[string]string{"id":bcv.Id,
                        "book":bcv.Book,
                        "chapter":strconv.Itoa(bcv.Chapter),
                        "verse":strconv.Itoa(bcv.Verse),
                        "text":bcv.Text}
}

type Bible struct {
    Books map[string]vector.Vector
}

func NewBible(loc string) (bible *Bible){
    bible = &Bible{map[string]vector.Vector{}}
    f,_:=ioutil.ReadFile(loc)
    lines := strings.Split(string(f),"\n",-1)
    bookNum := 0
    chapNum := 0
    verseNum := 0
    var bookName string
    for _,line := range lines {
        //new book
        if strings.Contains(line,"Book") {
            line = strings.TrimSpace(line)
            lineSplit:=strings.Split(line," ",-1)
            bookNum,_ = strconv.Atoi(lineSplit[0])
            bookName = lineSplit[1]
            bible.Books[bookName] = vector.Vector{}
        }else{
            line = strings.TrimSpace(line)
            if line != "" {
                lineSplit := strings.Split(line," ",-1)
                if len(lineSplit) > 1 && strings.Contains(lineSplit[0],":") {
                    verseText := strings.TrimSpace(lineSplit[2])
                    chapterVerse := strings.Split(lineSplit[0],":",-1)
                    chapNum,_ = strconv.Atoi(chapterVerse[0])
                    verseNum,_ = strconv.Atoi(chapterVerse[1])
                    vec,_:=bible.Books[bookName]
                    vec.Push(Bcv{
                            fmt.Sprintf("%d-%d-%d",bookNum,chapNum,verseNum),
                            bookName,chapNum,verseNum,verseText})
                }else{
                    vec,_:=bible.Books[bookName]
                    bcv:=vec.Pop().(Bcv) 
                    bcv.Text += " " + line 
                    vec.Push(bcv)

                }
            }

        }
    }
    return
}

func main() {
    bible := NewBible("kjv.txt")
    fmt.Printf("%s",bible)
}


